﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessModel;
using BusinessModel.EntityModel;
using BusinessModel.StudyManager;
using DataBaseAccessLib;

namespace SDZJ.ManagerView.StudyManager
{
    public partial class CourseWareCateManager : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
            }
        }

        #region MyRegion
        private void BindData()
        {
            CourseWareCateHelper courseWareCateHp = new CourseWareCateHelper();
            List<CourseWareCateEn> resLstCourseWareCateEn = courseWareCateHp.GetAllCourseWareCateEn();
            tvCourseCate.Nodes.Clear();
            GenerateTreeNodes(resLstCourseWareCateEn, null, tvCourseCate.Nodes);
        }

        private void GenerateTreeNodes(List<CourseWareCateEn> lstCourseWareCate, string parentID, TreeNodeCollection tnColl)
        {
            List<CourseWareCateEn> lstSubCourseWareCate = lstCourseWareCate.FindAll(delegate(CourseWareCateEn cEn) { return cEn.parent_id == parentID; });
            //进行排序
            var lstSortCourseWareCate = from subEn in lstSubCourseWareCate orderby subEn.indexno ascending select subEn;
            foreach (CourseWareCateEn cEn in lstSortCourseWareCate)
            {
                TreeNode childNode = new TreeNode();
                string treeViewText = string.Format("{0} <a href='javascript:AddCourseCate(\"{0}\",\"{1}\")' style='color:#4444FF'>[添加]</a>&nbsp;&nbsp;<a href='javascript:EditCourseCate(\"{0}\",\"{1}\",\"{2}\",\"{3}\",\"{4}\")' style='color:#4444FF'>[编辑]</a>&nbsp;&nbsp;<a href='javascript:DeleteCourseCate(\"{1}\")' style='color:#4444FF'>[删除]</a>"
                    , cEn.name, cEn.category_id, cEn.parent_id, cEn.indexno, cEn.note);
                childNode.Text = treeViewText;
                childNode.ToolTip = cEn.note;
                GenerateTreeNodes(lstCourseWareCate, cEn.category_id, childNode.ChildNodes);

                tnColl.Add(childNode);

            }
        }
        #endregion

        protected void btnOK_Click(object sender, EventArgs e)
        {
            string updateType = this.hidUpdateType.Value.ToUpper();
            string cateID = this.hidCateID.Value;
            string name = this.txtCateName.Text;
            string note = this.txtMemo.Text;
            string parentID = this.hidParentID.Value;
            int indexNO;
            Int32.TryParse(this.txtIndexNO.Text, out indexNO);

            CourseWareCateEn updateEn = new CourseWareCateEn();
            updateEn.name = name;
            updateEn.note = note;
            updateEn.indexno = indexNO;

            CourseWareCateHelper ccHp = new CourseWareCateHelper();
            string updateRes = "系统错误，请重试！";
            switch (updateType)
            {
                case "ADD":
                    updateEn.category_id = OConfig.GetGUID();
                    updateEn.parent_id = cateID;
                    updateRes = ccHp.AddCourseWareCate(updateEn);
                    break;
                case "UPDATE":
                    updateEn.category_id = cateID;
                    updateEn.parent_id = parentID;
                    updateRes = ccHp.EditCourseWareCate(updateEn);
                    break;
                default:
                    updateRes = "系统错误，请重试！";
                    break;
            }
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "", "showreuslt('" + updateRes + "');", true);
        }

        protected void lnkDeleteCourseCate_Click(object sender, EventArgs e)
        {
            string cateID = hidCateID.Value;

            CourseWareCateHelper ccHp = new CourseWareCateHelper();
            string msg = ccHp.DeleteCourseWareCate(cateID);

            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "", "alert('" + msg + "');RefData();", true);
        }

        protected void lnkRefData_Click(object sender, EventArgs e)
        {
            BindData();
        }
    }

}